package com.cuanbobox.tcp.matrix;


import com.cuanbobox.base.App;
import com.cuanbobox.bean.DataBean;
import com.cuanbobox.bean.DataList;
import com.cuanbobox.utils.LogUtil;

/**
 * 矩阵心跳包发送线程
 */
public class MatrixHeartbeatThread extends Thread {
    private boolean loop;
    private MatrixInitThread tcpInitThread;
    public static final String heartbeat = "#JSON:weburl,0.";
    public static final long interval = 6000;

    public MatrixHeartbeatThread(MatrixInitThread tcpInitThread) {
        this.tcpInitThread = tcpInitThread;
    }

    @Override
    public void run() {
        super.run();
        loop = true;
        LogUtil.add("心跳包", "启动心跳包线程-->" + Thread.currentThread().getName(), "矩阵");
        while (App.running && loop&&!App.debugMode) {
            if (!DataList.containHeartbeatPackage()) {
                if (App.readingMatrixInfo) {
                    DataList.add(new DataBean("#JSON:video,0."));
                } else {
                    DataList.add(new DataBean(heartbeat));
                }
            }
            try {
                Thread.sleep(interval);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        LogUtil.add("心跳包", "关闭心跳包线程-->" + Thread.currentThread().getName(), "矩阵");
    }

    public void stopThread() {
        loop = false;
    }
}
